package com.jinbiao.quasar.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.dto.DeptDto;
import com.jinbiao.quasar.dto.DeptUserDto;
import com.jinbiao.quasar.dto.UserDto;
import com.jinbiao.quasar.model.SysDept;
import com.jinbiao.quasar.model.SysDeptUser;
import com.jinbiao.quasar.model.SysUser;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityQuery;
import org.sagacity.sqltoy.model.Page;
import org.sagacity.sqltoy.model.QueryExecutor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@OperationLog
public class DeptController {
    @Db
    SqlToyLazyDao dao;

    @Mapping("/dept/get-dept-list")
    public AjaxJson getDeptList(int page, int pageSize, String sortBy, boolean desc) {
        Page<SysDept> pageResult = dao.findPageEntity(new Page(pageSize, page), SysDept.class, desc ? EntityQuery.create().orderByDesc(sortBy) : EntityQuery.create().orderBy(sortBy), SysDept.class);
        return AjaxJson.getSuccessData(pageResult);
    }



    @Mapping("/dept/delete-dept-by-id")
    @Tran
    public AjaxJson deleteDeptById(SysDept sysDept) {
        dao.deleteByQuery(SysDeptUser.class, EntityQuery.create().where("sys_dept_dept_code in (select dept_code from sys_dept where id = ?)").values(sysDept.getDeptCode()));
        return AjaxJson.getSuccessData(dao.delete(sysDept));
    }

    @Mapping("/dept/edit-dept")
    public AjaxJson editDept(SysDept sysDept) {
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysDept));
    }

    @Mapping("/dept/add-dept")
    public AjaxJson addDept(SysDept sysDept) {
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysDept));
    }

    @Mapping("/dept/query-dept-by-id")
    public AjaxJson queryDeptById(DeptDto sysDept) {
        List<DeptDto> results = dao.findBySql("sysDeptList", BeanUtil.beanToMap(sysDept, "id"), DeptDto.class);
        if (CollectionUtil.isNotEmpty(results)) {
            DeptDto dept = results.get(0);
            final Map<String, Object> params = new HashMap<>();
            params.put("username", dept.getLeader());
            List<SysUser> users = dao.findBySql("sysUserList", params, SysUser.class);
            if (CollectionUtil.isNotEmpty(users)) dept.setLeaderUser(users.get(0));
            return AjaxJson.getSuccessData(dept);
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/public/get-dept-all")
    public AjaxJson getDeptAll(SysDept sysDept) {
        return AjaxJson.getSuccessData(dao.findBySql("sysDeptList", sysDept));
    }

    @Mapping("/dept/query-user-by-dept")
    public AjaxJson queryUserByDept(int page, int pageSize, String sortBy, boolean desc, SysDept sysDept) {
        Page<SysUser> pageResult = dao.findPageBySql(new Page(pageSize, page), "queryUserByDept", BeanUtil.beanToMap(sysDept, "deptCode"), SysUser.class);
        pageResult.getRows().stream().forEach(item -> {
            item.setPassword(null);
        });
        return AjaxJson.getSuccessData(pageResult);
    }

    @Mapping("/dept/add-dept-user")
    @Tran
    public AjaxJson addDeptUser(DeptUserDto deptUserDto) {
        List<SysDeptUser> deptUsers = new ArrayList<>();
        if (!CollectionUtil.isNotEmpty(deptUserDto.getUsernames())) return AjaxJson.getError("传入参数错误");
        for (String username : deptUserDto.getUsernames())
            deptUsers.add(new SysDeptUser(deptUserDto.getDeptCode(), username));
        return AjaxJson.getSuccessData(dao.saveAll(deptUsers));
    }

    @Mapping("/dept/remove-dept-user")
    @Tran
    public AjaxJson removeDeptUser(SysDeptUser deptUser) {
        return AjaxJson.getSuccessData(dao.deleteByQuery(SysDeptUser.class, EntityQuery.create().where(" #[and sys_dept_dept_code=:deptCode]" +
                "            #[and sys_user_username=:username]").values(deptUser)));
    }
}
